package com.app.utils;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.utils.impl.FunctionManager;
import com.app.utils.impl.IConsole;
import com.app.utils.impl.labour.AppUtilStrategy;
import com.app.utils.impl.labour.AssetsUtilStrategy;
import com.app.utils.impl.labour.CleanerUtilStrategy;
import com.app.utils.impl.labour.ClipboardUtilStrategy;
import com.app.utils.impl.labour.CollectUtilStrategy;
import com.app.utils.impl.labour.CryptoUtilStrategy;
import com.app.utils.impl.labour.DeviceUtilStrategy;
import com.app.utils.impl.labour.FileUtilStrategy;
import com.app.utils.impl.labour.NetUtilStrategy;
import com.app.utils.impl.labour.NotchUtilStrategy;
import com.app.utils.impl.labour.PatternUtilStrategy;
import com.app.utils.impl.labour.ProcessUtilStrategy;
import com.app.utils.impl.labour.RawUtilStrategy;
import com.app.utils.impl.labour.RomUtilStrategy;
import com.app.utils.impl.labour.ScreenUtilStrategy;
import com.app.utils.impl.labour.StorageUtilStrategy;
import com.app.utils.impl.labour.StringUtilStrategy;
import com.app.utils.impl.labour.TimerUtilStrategy;
import com.app.utils.spannable.SpannableActivity;
import com.app.utils.statusbar.StatueBarActivity;
import com.zndroid.utils.XUtils;

public class MainActivity extends AppCompatActivity implements IConsole {
    private AppCompatSpinner spinner1;
    private RecyclerView recyclerView;
    private TxtAdapter adapter;

    private String funName = "";
    private FunctionManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initRecyclerView();

        spinner1 = findViewById(R.id.sp_1);

        manager = FunctionManager.getInstance();

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                funName = (String) parent.getSelectedItem();
                doFunction(funName);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //可选操作 for test
        XUtils.create().uniformContext(this);
    }

    private void doFunction(String funName) {
        switch (funName) {
            case "AppUtil":
                manager.doSomething(new AppUtilStrategy(this));
                break;
            case "AssetsUtil":
                manager.doSomething(new AssetsUtilStrategy(this));
                break;
            case "CleanerUtil":
                manager.doSomething(new CleanerUtilStrategy(this));
                break;
            case "CollectUtil":
                manager.doSomething(new CollectUtilStrategy(this));
                break;
            case "DeviceUtil":
                manager.doSomething(new DeviceUtilStrategy(this));
                break;
            case "CryptoUtil":
                manager.doSomething(new CryptoUtilStrategy(this));
                break;
            case "PatternUtil":
                manager.doSomething(new PatternUtilStrategy(this));
                break;
            case "NetUtil":
                manager.doSomething(new NetUtilStrategy(this));
                break;
            case "ProcessUtil":
                manager.doSomething(new ProcessUtilStrategy(this));
                break;
            case "ScreenUtil":
                manager.doSomething(new ScreenUtilStrategy(this));
                break;
            case "FileUtil":
                manager.doSomething(new FileUtilStrategy(this));
                break;
            case "StorageUtil":
                manager.doSomething(new StorageUtilStrategy(this));
                break;
            case "RawUtil":
                manager.doSomething(new RawUtilStrategy(this));
                break;
            case "ClipboardUtil":
                manager.doSomething(new ClipboardUtilStrategy(this));
                break;
            case "RomUtil":
                manager.doSomething(new RomUtilStrategy(this));
                break;
            case "NotchUtil":
                manager.doSomething(new NotchUtilStrategy(this, this));
                break;
            case "TimeUtil":
                manager.doSomething(new TimerUtilStrategy(this));
                break;
            case "StringUtil":
                manager.doSomething(new StringUtilStrategy(this));
                break;
            default:
                break;
        }
    }

    private void initRecyclerView() {
        recyclerView = findViewById(R.id.rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //从下往上刷新数据
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new TxtAdapter();
        recyclerView.setAdapter(adapter);
    }

    public void jumpToStatueBarAct(View view) {
        startActivity(new Intent(this, StatueBarActivity.class));
    }

    public void jumpToSpannableAct(View view) {
        startActivity(new Intent(this, SpannableActivity.class));
    }

    public void clean(View view) {
        adapter.clearAll();
    }

    @Override
    public void console(String key, String value) {
        key = ">>> " + key;

        SpannableString spannableString = new SpannableString(key + "\n" + value);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#17BB56")), key.length() + 1, spannableString.length(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);

        adapter.appendTxt(spannableString);
        if(adapter.getItemCount() > 0) {
            recyclerView.scrollToPosition(adapter.getItemCount() - 1);
        }
    }
}